package com.ruoyi.standingbook.v2toilet.service.impl;

import cn.hutool.core.bean.BeanUtil;
import com.ruoyi.common.utils.BeanCopyUtils;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.core.domain.PageQuery;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;
import com.ruoyi.standingbook.v2toilet.domain.bo.V2ToiletImpDetailBo;
import com.ruoyi.standingbook.v2toilet.domain.vo.V2ToiletImpDetailVo;
import com.ruoyi.standingbook.v2toilet.domain.V2ToiletImpDetail;
import com.ruoyi.standingbook.v2toilet.mapper.V2ToiletImpDetailMapper;
import com.ruoyi.standingbook.v2toilet.service.IToiletImpDetailService;

import java.util.List;
import java.util.Map;
import java.util.Collection;

/**
 * V2市政公厕管养Service业务层处理
 *
 * @author cc
 * @date 2023-06-07
 */
@RequiredArgsConstructor
@Service
public class V2ToiletImpDetailServiceImpl implements IToiletImpDetailService {

    private final V2ToiletImpDetailMapper baseMapper;

    /**
     * 查询V2市政公厕管养
     */
    @Override
    public V2ToiletImpDetailVo queryById(String id) {
        return baseMapper.selectVoById(id);
    }

    /**
     * 查询V2市政公厕管养列表
     */
    @Override
    public TableDataInfo<V2ToiletImpDetailVo> queryPageList(V2ToiletImpDetailBo bo, PageQuery pageQuery) {
        LambdaQueryWrapper<V2ToiletImpDetail> lqw = buildQueryWrapper(bo);
        Page<V2ToiletImpDetailVo> result = baseMapper.selectVoPage(pageQuery.build(), lqw);
        return TableDataInfo.build(result);
    }

    /**
     * 查询V2市政公厕管养列表
     */
    @Override
    public List<V2ToiletImpDetailVo> queryList(V2ToiletImpDetailBo bo) {
        LambdaQueryWrapper<V2ToiletImpDetail> lqw = buildQueryWrapper(bo);
        return baseMapper.selectVoList(lqw);
    }

    private LambdaQueryWrapper<V2ToiletImpDetail> buildQueryWrapper(V2ToiletImpDetailBo bo) {
        Map<String, Object> params = bo.getParams();
        LambdaQueryWrapper<V2ToiletImpDetail> lqw = Wrappers.lambdaQuery();
        lqw.eq(StringUtils.isNotBlank(bo.getBatchNo()), V2ToiletImpDetail::getBatchNo, bo.getBatchNo());
        lqw.eq(bo.getCreateDate() != null, V2ToiletImpDetail::getCreateDate, bo.getCreateDate());
        lqw.eq(StringUtils.isNotBlank(bo.getCommunity()), V2ToiletImpDetail::getCommunity, bo.getCommunity());
        lqw.like(StringUtils.isNotBlank(bo.getToiletName()), V2ToiletImpDetail::getToiletName, bo.getToiletName());
        lqw.eq(StringUtils.isNotBlank(bo.getToiletCode()), V2ToiletImpDetail::getToiletCode, bo.getToiletCode());
        return lqw;
    }

    /**
     * 新增V2市政公厕管养
     */
    @Override
    public Boolean insertByBo(V2ToiletImpDetailBo bo) {
        V2ToiletImpDetail add = BeanUtil.toBean(bo, V2ToiletImpDetail.class);
        validEntityBeforeSave(add);
        boolean flag = baseMapper.insert(add) > 0;
        if (flag) {
            bo.setId(add.getId());
        }
        return flag;
    }

    @Override
    public Boolean batchInsertByBo(List<V2ToiletImpDetailVo> boList) {
        List<V2ToiletImpDetail> list = BeanCopyUtils.copyList(boList, V2ToiletImpDetail.class);
        return baseMapper.insertBatch(list);
    }

    @Override
    public boolean batchInsertOrUpdate(List<V2ToiletImpDetailVo> boList) {
        List<V2ToiletImpDetail> list = BeanCopyUtils.copyList(boList, V2ToiletImpDetail.class);
        return baseMapper.insertOrUpdateBatch(list);
    }

    /**
     * 修改V2市政公厕管养
     */
    @Override
    public Boolean updateByBo(V2ToiletImpDetailBo bo) {
        V2ToiletImpDetail update = BeanUtil.toBean(bo, V2ToiletImpDetail.class);
        validEntityBeforeSave(update);
        return baseMapper.updateById(update) > 0;
    }

    /**
     * 保存前的数据校验
     */
    private void validEntityBeforeSave(V2ToiletImpDetail entity) {
        //TODO 做一些数据校验,如唯一约束
    }

    /**
     * 批量删除V2市政公厕管养
     */
    @Override
    public Boolean deleteWithValidByIds(Collection<String> ids, Boolean isValid) {
        if (isValid) {
            //TODO 做一些业务上的校验,判断是否需要校验
        }
        //return baseMapper.deleteBatchIds(ids) > 0;
        return baseMapper.realDelByIds(ids) > 0;
    }
}
